package com.szalay.useradmin;

import java.util.ArrayList;
import java.util.List;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.service.useradmin.Authorization;
import org.osgi.service.useradmin.Group;
import org.osgi.service.useradmin.Role;
import org.osgi.service.useradmin.UserAdmin;

/**
 * Authorization implementation.
 */
final class AuthorizationImpl implements Authorization {
    private final String user;
    private final UserAdmin parent;
    
    /**
     * Create a new authorization impl.
     * @param user the user
     * @param parent the user admin parent.
     */
    AuthorizationImpl(final String user, final UserAdmin parent) {
        this.user = user;
        this.parent = parent;
    }
    
    @Override
    public String getName() {
        return user;
    }

    @Override
    public boolean hasRole(final String roleName) {
        if (roleName == null) {
            return false;
        }
        
        try {
            //Get all groups
            final Role[] allRoles = parent.getRoles(null);
            for (final Role role : allRoles) {
                if (role.getType() == Role.GROUP) {
                    final Group group = (Group) role;
                    
                    //If the group contains this user as a member, check if the group contains the role...
                    boolean groupHasThisMember = false;
                    for (final Role member : group.getMembers()) {
                        if (member.getName().equals(user)) {
                            groupHasThisMember = true;
                            break;
                        }
                    }
                    
                    if (groupHasThisMember) {
                        for (final Role aRole : group.getMembers()) {
                            if (aRole.getType() == Role.ROLE) {
                                if (aRole.getName().equals(roleName)) {
                                    return true;
                                }
                            }
                        }
                    }
                }
            }
            return false;
        } 
        catch (InvalidSyntaxException ex) {
            throw new IllegalStateException(ex);
        }
    }

    @Override
    public String[] getRoles() {
        try {
            //Get all groups
            final List<String> result = new ArrayList<String>();
            final Role[] allRoles = parent.getRoles(null);
            for (final Role role : allRoles) {
                if (role.getType() == Role.GROUP) {
                    final Group group = (Group) role;
                    
                    //If the group contains this user as a member, check if the group contains the role...
                    boolean groupHasThisMember = false;
                    for (final Role member : group.getMembers()) {
                        if (member.getName().equals(user)) {
                            groupHasThisMember = true;
                            break;
                        }
                    }
                    
                    if (groupHasThisMember) {
                        for (final Role aRole : group.getMembers()) {
                            if (aRole.getType() == Role.ROLE) {
                                result.add(aRole.getName());
                            }
                        }
                    }
                }
            }
            return result.toArray(new String[] { });
        } 
        catch (InvalidSyntaxException ex) {
            throw new IllegalStateException(ex);
        }
    }
}
